package util;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class Strings
{
    public static final String validPhoneChars = "0123456789 -";
    public static final String validNamesChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
    public static final String validProblemNamesChars = "\tABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.0123456789,";
    public static final String validHandlesChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz()_.@";
    public static final String validPasswordChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    public static final String validEmailChars = "abcdefghijklmnopqrstuvwxyz._0123456789";
    public static final int PAGE_MAX = 35;
    public static String capitalizeString( String c ){
        return Character.toUpperCase(c.charAt(0)) + c.substring(1);
    }
    public static boolean isValidHandle( String h )
    {
        if( h.length() > 30 )return false;
        if( h.length() == 0 )return false;
        for (int i = 0; i < h.length(); i++)
            if( !validHandlesChars.contains(h.charAt(i)+"") )
                return false;
        return true;
    }
    public static boolean isValidProblemName( String h )
    {
        if( h.length() > 40 )return false;
        if( h.length() == 0 )return false;
        for (int i = 0; i < h.length(); i++)
            if( !validProblemNamesChars.contains(h.charAt(i)+"") )
                return false;
        return true;
    }
    public static boolean isValidName( String name )
    {
        if( name.length() > 100 )return false;
        if( name.length() == 0 )return false;
        for (int i = 0; i < name.length(); i++)
            if( !validNamesChars.contains(name.charAt(i)+"") )
                return false;
        return true;
    }
    public static String capitalizeStrings( String c )
    {
        String[] a = c.split("[ ]+");
        String r = "";
        for (int i = 0; i < a.length; i++) {
            r += capitalizeString( a[i] )+" ";
        }
        return r.trim();
    }
    public static final String none = "-------------------";
    public static boolean isInteger( String str )
    {
        if( str.length() > 10 )return false;
        if( str.length() == 0 )return false;
        for( char a : str.toCharArray() )
            if( !Character.isDigit( a ) )
                return false;
        return true;
    }
    public static boolean isPositiveDouble( String string )
    {
        if( string.length() > 20 )return false;
        if( string.length() == 0 )return false;
        String str = new String(string);
        if( str.length() == 0 )return false;
        int points = 0;
        str = str.trim();
        for (int i = 0; i < str.length(); i++)
        {
            if( str.charAt(i) == '.')
                ++points;
            else if( !Character.isDigit(str.charAt(i)))
                return false;
        }
        return points == 1;
    }
    public static boolean isValidPassword( String str )
    {
        if( str.length() > 12 )return false;
        if( str.length() < 4 )
            return false;
        for( char c : str.toCharArray() )
        {
            if( !Character.isDigit(c) && !Character.isLetter(c) )
                return false;
        }
        return true;
    }

    public static int count( String str , char c )
    {
        int r = 0;
        for( int i = 0; i < str.length(); ++i)
            if( str.charAt(i) == c )
                ++r;
        return r;
    }

    public static boolean isValidEmailAddress( String str ) {

        if( str.length() == 0 ) return false;
        if( count(str, '@') != 1 )return false;
        if( str.charAt(0) == '@' || str.charAt( str.length() - 1 ) == '@' )return false;
        if( str.contains(" ") )return false;
        String arr[] = str.split("@");
        String a = arr[0];
        String b = arr[1];
        if( a.length() == 0 || b.length() == 0 )return false;

        if( !Character.isLetterOrDigit(a.charAt(0)) || !Character.isLetterOrDigit(a.charAt( a.length() - 1 )) )
            return false;

        for (int i = 0; i < str.length(); i++)
        {
            if( str.charAt(i) == '@' )continue;
            if( validEmailChars.indexOf( str.charAt(i) ) < 0 )
                return false;
        }

        for( char c: b.toCharArray() )
            if( c != '.' && !Character.isLetter(c))
                return false;

        if( a.contains(".."))
            return false;

        return true;
    }

    public static boolean isValidPhone(String str) {
        if( str.length() == 0 )return false;
        str = str.replaceAll(" -", "");
        for( char c : str.toCharArray() )
            if( !Character.isDigit(c) )return false;
        return true;
    }
    public static String format(double numero, int decimales)
    {
        //de esta forma, si numero = 0.00912 < 1 y decimales == 3, saldrá 0.009
        if (numero < 0) {
            return "-" + format(-numero, decimales);
        }
        String r = ".", rta = "";
        for (int i = 0; i < decimales; i++) {
            r += "0";
        }
        if (numero < 1) {
            rta += "0";
        }
        rta += (new DecimalFormat(r)).format(round(numero, decimales)).replaceAll(",", ".");
        return rta;
    }
    public static double round(double numero, int decimales)
    {
        return Math.round(numero * Math.pow(10, decimales)) / Math.pow(10, decimales);
    }
    public static int[] getAlignmentArray( ArrayList<Object> row )
    {
        if( row == null )return null;
        int r[] = new int[row.size()];
        for (int i = 0; i < r.length; i++)
        {
            if( row.get(i) == null )
                r[i] = SwingConstants.RIGHT;
            else
                r[i] = row.get(i) instanceof Integer || row.get(i) instanceof Double ? SwingConstants.RIGHT : Strings.isPositiveDouble( row.get(i).toString())  ? SwingConstants.RIGHT :SwingConstants.LEFT;
        }
        return r;
    }

    public static void setCodeTextArea(JTextArea codeTA) {
        codeTA.setTabSize(3);
        codeTA.setFont( new Font("Courier new", Font.PLAIN , 14));
        codeTA.setForeground(Color.GREEN);
        codeTA.setBackground(Color.BLACK);
    }
    public static void setProblemDescriptionTextArea(JTextArea codeTA) {
        codeTA.setTabSize(3);
        codeTA.setFont( new Font("Courier new", Font.PLAIN , 14));
    }
}
